﻿using App.Code;
using App.Entity;
using App.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AppAit.Areas.SystemSecurity.Controllers
{
    public class DbBackupController : ControllerBase
    {
       ISys_DbBackupServices _iDbBackupServices;
        public DbBackupController(ISys_DbBackupServices iDbBackupServices)
        {
            this._iDbBackupServices = iDbBackupServices;
        }
      

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJson(string queryJson)
        {
            var data = _iDbBackupServices.GetList(queryJson);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HandlerAjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SubmitForm(Sys_DbBackup dbBackupEntity)
        {
            FileHelper.CreateDirectory(Server.MapPath("~/Resource/DbBackup"));
            dbBackupEntity.FilePath = Server.MapPath("~/Resource/DbBackup/" + dbBackupEntity.FileName + ".bak");
            dbBackupEntity.FileName = dbBackupEntity.FileName + ".bak";
            _iDbBackupServices.SubmitForm(dbBackupEntity);
            return Success("操作成功。");
        }
        /// <summary>
        /// 删除备份
        /// </summary>
        [HttpPost]
        [HandlerAjaxOnly]
        [HandlerAuthorize]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteForm(string keyValue)
        {
            _iDbBackupServices.DeleteForm(keyValue);
            return Success("删除成功。");
        }
        /// <summary>
        /// 下载备份
        /// </summary>
        /// <param name="keyValue"></param>
        [HttpPost]
        [HandlerAuthorize]
        public void DownloadBackup(string keyValue)
        {
            var data = _iDbBackupServices.GetForm(keyValue);
            string filename = Server.UrlDecode(data.FileName);
            string filepath = Server.MapPath(data.FilePath);
            if (FileDownHelper.FileExists(filepath))
            {
                FileDownHelper.DownLoadold(filepath, filename);
            }
        }
    }
}